package com.wxzz.elearing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxzz.elearing.user.dto.BannerManageDto;
import com.wxzz.elearing.user.entity.BannerManage;
import com.wxzz.elearing.user.mapper.BannerManageMapper;
import com.wxzz.elearing.user.service.IBannerManageService;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerManageImpl extends ServiceImpl<BannerManageMapper, BannerManage> implements IBannerManageService {

    @Autowired
    private BannerManageMapper bannerManageMapper;

    @Override
    public ReturnVO saveBannerManage(BannerManage bannerManage) {
        bannerManageMapper.insert(bannerManage);
        return new ReturnVO("banner新增成功", bannerManage.getId());
    }

    @Override
    public ReturnVO updateBannerManage(BannerManage bannerManage) {
        bannerManageMapper.updateById(bannerManage);
        return new ReturnVO("banner修改成功", bannerManage.getId());
    }

    @Override
    public ReturnVO deleteBannerManage(Long bannerManageId) {
        bannerManageMapper.deleteById(bannerManageId);
        return new ReturnVO("banner删除成功", bannerManageId);
    }

    @Override
    public ReturnVO<List<BannerManage>> listBannerManage(Long enterpriseId) {
        List<BannerManage> bannerManages = bannerManageMapper.selectList(new LambdaQueryWrapper<BannerManage>()
                .eq(BannerManage::getEnterpriseId, enterpriseId)
                .orderByAsc(BannerManage::getSort));
        return new ReturnVO("banner展示成功", bannerManages);
    }

    @Override
    public ReturnVO<List<BannerManage>> listBannerManageByModel(Long enterpriseId, BannerManageDto bannerManageDto) {
        QueryWrapper<BannerManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sort");
        queryWrapper.eq("enterprise_id",enterpriseId);
        if (bannerManageDto.getModelType() != null) {
            queryWrapper.eq("model_type",bannerManageDto.getModelType());
        }
        List<BannerManage> bannerManages = bannerManageMapper.selectList(queryWrapper);
        return new ReturnVO("banner展示成功", bannerManages);
    }
}
